package com.snap.vseries.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.snap.vseries.Constants;
import com.snap.vseries.R;
import com.snap.vseries.base.BaseActivity;
import com.snap.vseries.manager.ConfigManager;
import com.snap.vseries.ui.widget.DialogView;
import com.snap.vseries.ui.widget.TextButton;
import com.snap.vseries.utils.NumberLimitUtils;
import com.snap.vseries.utils.ToastUtils;
import com.snap.vseries.utils.TopTitleUtils;

import java.text.DecimalFormat;
import java.util.Arrays;

import butterknife.BindView;
import butterknife.BindViews;
import butterknife.OnClick;

/**
 * 荧光串扰
 */
public class AmpCrossActivity extends BaseActivity {
    private static final String TAG = "AmpCrossActivity";

    @BindViews({R.id.tv_1_1, R.id.tv_1_2, R.id.tv_1_3, R.id.tv_1_4,
            R.id.tv_2_1, R.id.tv_2_2, R.id.tv_2_3, R.id.tv_2_4,
            R.id.tv_3_1, R.id.tv_3_2, R.id.tv_3_3, R.id.tv_3_4,
            R.id.tv_4_1, R.id.tv_4_2, R.id.tv_4_3, R.id.tv_4_4})
    TextView[] tvs;
    @BindView(R.id.save_setting_btn)
    TextButton saveSettingBtn;
    /**
     * 保存前的数据
     */
    private StringBuffer before = new StringBuffer();
    private double[] crossTalk;

    @Override
    protected int layoutResId() {
        return R.layout.activity_amp_cross;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new TopTitleUtils(this).setTitle("荧光串扰").setLeft(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        double[] set = ConfigManager.getInstance().getSettingInfo().getCrossTalk();
        if (set == null) {
            set = new double[16];
        }
        if (set == null || set.length != 16) {
            return;
        }
        crossTalk = set.clone();
        Log.e(TAG, "onCreate: " + Arrays.toString(crossTalk));
        for (int i = 0; i < crossTalk.length; i++) {
            final int index = i;
            before.append(crossTalk[i]).append(",");
            tvs[i].setText(String.valueOf(crossTalk[i]));
            if (i % 5 == 0) {
                tvs[i].setTextColor(0xFFC1C3CF);
                continue;
            }
            tvs[i].setTextColor(0xFF568BFF);
            tvs[i].setOnClickListener(v -> {
                DialogView dialogView = new DialogView(this);
                dialogView.setTitle("请输入数值");
                dialogView.setContentType(true);
                dialogView.setClickSureDismissDialog(false);
                dialogView.setEditContent(String.valueOf(crossTalk[index]));
                dialogView.setOnClickListener(content -> {
                    if (!NumberLimitUtils.numberLimit(content, "1", "0")) {
                        return;
                    }
                    try {
                        content = new DecimalFormat("0.###").format(Double.valueOf(content));
                    } catch (NumberFormatException e) {
                        ToastUtils.get().showText("输入的值不符合规范");
                        return;
                    }
                    crossTalk[index] = Double.valueOf(content);
                    tvs[index].setText(String.valueOf(crossTalk[index]));
                    dialogView.dismiss();
                });
                dialogView.show();
            });
        }

        saveSettingBtn.setOnClickListener(v -> {
            if (contentIsChanged()) {
                save();
                ToastUtils.get().showText("保存成功");
                finish();
            } else {
                ToastUtils.get().showText("请输入要修改的内容");
            }
        });
    }

    private void save() {
        ConfigManager.getInstance().getSettingInfo().setCrossTalk(crossTalk);
        ConfigManager.getInstance().update();

        before = new StringBuffer();
        for (int i = 0; i < crossTalk.length; i++) {
            before.append(crossTalk[i]).append(",");
        }
    }

    private boolean contentIsChanged() {
        StringBuffer now = new StringBuffer();
        for (int i = 0; i < crossTalk.length; i++) {
            now.append(crossTalk[i]).append(",");
        }
        return !before.toString().equals(now.toString());
    }

    @OnClick({R.id.leftWrapper})
    public void click(View view) {
        switch (view.getId()) {
            case R.id.leftWrapper:
                onBack();
                break;
            default:
        }
    }

    public void onBack() {
            new DialogView(this)
                    .setTitle("提示")
                    .setTextContent("已修改没保存，是否保存？")
                    .setOnClickListener(content -> {
                        save();
                        ToastUtils.get().showText("保存成功");
                        finish();
                    })
                    .setOnClickListener(this::finish)
                    .show();
        }

}
